package com.upc.feedish.utils;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandRequest;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandResponse;
import com.huaweicloud.sdk.iotda.v5.model.DeviceCommandRequest;
import com.huaweicloud.sdk.iotda.v5.model.ShowDeviceShadowRequest;
import com.huaweicloud.sdk.iotda.v5.model.ShowDeviceShadowResponse;
import com.huaweicloud.sdk.iotda.v5.model.UpdateDesired;
import com.huaweicloud.sdk.iotda.v5.model.UpdateDesireds;
import com.huaweicloud.sdk.iotda.v5.model.UpdateDeviceShadowDesiredDataRequest;
import com.huaweicloud.sdk.iotda.v5.model.UpdateDeviceShadowDesiredDataResponse;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class HuaweiCloudTools {
    public static final String AK="4FYKX04ALY6VXEZHSDVE";
    public static final String SK="tkkO8BTi7rSIxoSvxvfXh0FtnjupkC86OUUoXqCB";
    public static final String SERVICE_ID = "Agriculture";
    public static final String DEVICE_ID = "6097c400aa3bcc02c01eebca_12345678";


    public static LinkedHashMap<String,Object> getProperties() {
        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(AK)
                .withSk(SK);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.CN_NORTH_4)
                .build();
        // 实例化请求对象
        ShowDeviceShadowRequest request = new ShowDeviceShadowRequest();
        request.setDeviceId(DEVICE_ID);
        try {
            // 调用查询设备列表接口
            ShowDeviceShadowResponse response = client.showDeviceShadow(request);
            LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>) response
                    .getShadow()
                    .get(0)
                    .getReported()
                    .getProperties();
            return linkedHashMap;
        } catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void postCommand(String commandName,String paraName,String paraValue){
        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(AK)
                .withSk(SK);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.CN_NORTH_4)
                .build();
        // 实例化请求对象
        CreateCommandRequest request =new CreateCommandRequest();
        DeviceCommandRequest dRequest=new DeviceCommandRequest();
        Map<String,String> parasMap=new HashMap<String,String>();
        parasMap.put(paraName,paraValue);
        dRequest.setCommandName(commandName);
        dRequest.setParas(parasMap);
        dRequest.setServiceId(SERVICE_ID);
        request.setBody(dRequest);
        request.setDeviceId(DEVICE_ID);
        try {
            CreateCommandResponse ccr=client.createCommand(request);
//            ccr.getResponse();
        }catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) {
            e.printStackTrace();
        }
    }

    public static void updateSetting(String tep,String hum,String lum){
        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(AK)
                .withSk(SK);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.CN_NORTH_4)
                .build();
        UpdateDesireds updateDesireds = new UpdateDesireds();
        UpdateDesired updateDesiredTep = new UpdateDesired();
        UpdateDesired updateDesiredHum = new UpdateDesired();
        UpdateDesired updateDesiredLum = new UpdateDesired();
        String tepStr ="{" + "temperature"+":"+tep+"}";
        String humStr ="{" + "Humanity"+":"+hum+"}";
        String lumStr ="{" + "Luminance"+":"+lum+"}";
        updateDesiredTep.setDesired(tepStr);
        updateDesiredHum.setDesired(humStr);
        updateDesiredLum.setDesired(lumStr);
        updateDesiredTep.setServiceId(SERVICE_ID);
        updateDesiredHum.setServiceId(SERVICE_ID);
        updateDesiredLum.setServiceId(SERVICE_ID);
        List<UpdateDesired> shadowDesire = new ArrayList<>();
        shadowDesire.add(updateDesiredTep);
        shadowDesire.add(updateDesiredHum);
        shadowDesire.add(updateDesiredLum);
        updateDesireds.setShadow(shadowDesire);
        // 实例化请求对象
        UpdateDeviceShadowDesiredDataRequest request=new UpdateDeviceShadowDesiredDataRequest();
        request.setDeviceId(DEVICE_ID);
        request.setBody(updateDesireds);
        try {
            UpdateDeviceShadowDesiredDataResponse response=client.updateDeviceShadowDesiredData(request);
        }catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) {
            e.printStackTrace();
        }

    }

}
